package com.school.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.admin.common.annotation.DataAuthority;
import com.school.admin.common.imit.InitDict;
import com.school.admin.common.result.QueryVo;
import com.school.admin.common.result.R;
import com.school.admin.common.utils.BaseController;
import com.school.admin.entity.DishesDict;
import com.school.admin.entity.vo.DishesDictVo;
import com.school.admin.service.DishesDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author sunpx
 * @since 2021-08-05
 */
@RestController
@RequestMapping("/sys/dishesDict")
@Api(tags = "字典表")
@Slf4j
public class DishesDictController extends BaseController {

    @Autowired
    private DishesDictService iDishesDictService;


    @Autowired
    InitDict initDict;

    /**
     * 查询字典表列表
     *
     * @author sunpx
     * @since 2021-08-05
     */
    @PostMapping("queryDishesDictList")
    @ApiOperation("字典表列表")
    public R queryDishesDictList(@RequestBody QueryVo<DishesDict> queryVo) {
        Page<DishesDictVo> page = iDishesDictService.queryPage(queryVo.getPage(), queryVo.getQueryBean());
        return R.ok().data("dataList", page);
    }

    /**
     * 保存或者更新字典表信息
     *
     * @author sunpx
     * @since 2021-08-05
     */
    @PostMapping("saveOrUpdateDishesDict")
    @ApiOperation("保存或者更新字典表信息")
    public R saveOrUpdateDishesDict(@RequestBody DishesDict entity) {
        boolean b = true;
        if (StringUtils.isEmpty(entity.getId())) {
            b = iDishesDictService.save(entity);
        } else {
            b = iDishesDictService.updateById(entity);
        }
        if (b) {
            initDict.extracted();
        }


        return R.th(b);
    }


    /**
     * 根据id查询字典表信息
     *
     * @author sunpx
     * @since 2021-08-05
     */
    @GetMapping("getByIdDishesDict/{id}")
    @ApiOperation("根据id查询字典表信息")
    public R getByIdDishesDict(@PathVariable("id") String id) {
        DishesDict dataDishesDict = iDishesDictService.getById(id);
        return R.ok().data("entity", dataDishesDict);

    }

    @GetMapping("parameterList")
    @ApiOperation("参数列表")
    public R parameterList() {

        QueryWrapper<DishesDict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type", 1);
        List<DishesDict> list = iDishesDictService.list(dictQueryWrapper);

        HashMap<String, List<DishesDict>> listHashMap = new HashMap<>();
        for (DishesDict dishesDict : list) {
            List<DishesDict> dishesDictList = listHashMap.computeIfAbsent(dishesDict.getTypeCode(), k -> new ArrayList<>());
            dishesDictList.add(dishesDict);
        }

        Map<String, String> selectApi = getSelectApi();
        Set<String> strings = selectApi.keySet();
        List<Object> result = new ArrayList<>();
        for (String string : strings) {
            Map<Object, Object> map = new HashMap<>();
            map.put("typeCode", string);
            map.put("typeName", selectApi.get(string));
            map.put("parameterList", listHashMap.get(string));
            result.add(map);

        }

        return R.ok().data("dataList", result);
    }

    /**
     * 根据id删除字典表信息
     *
     * @author sunpx
     * @since 2021-08-05
     */
    @DeleteMapping("removeByIdDishesDict/{id}")
    @ApiOperation("根据id删除字典表信息")
    public R removeByIdDishesDict(@PathVariable("id") String id) {
        boolean effect = iDishesDictService.removeById(id);
        return R.th(effect);

    }


    @GetMapping("getDictType")
    @DataAuthority(name = "查看日志类型",key = 0)
    @ApiOperation("查看日志类型")
    public R getDictType() {
        QueryWrapper<DishesDict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type", 3);
        List<DishesDict> list = iDishesDictService.list(dictQueryWrapper);
        return R.ok().data("dataList", list);
    }
}

